import sys
import re
import chardet

def Usage():
    print """Usage:
        itemstart.py hostname 
    """
    pass

def gethost():
    if len(sys.argv)<2:
        Usage()
        return
    else:
        host = sys.argv[1]
        regex = re.compile(
        r'^(?:(?:http)s?://)?' # http:// or https://
        r'((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
        r'localhost|)' #localhost...
        r'(?::\d+)?' # optional port
        r'(?:/?|[/?]\S+)$', re.IGNORECASE)
        match = regex.match(host)
        if not match:
            print "invalid website"
            return 
        else:
            return match.group(1)
        
def contodict(query):
    if re.match(r'\S+=\S+&\S+=\S+',query):
        li=query.split('&')
        dict={}
        for item in li:
            a=item.split('=')
            code=chardet.detect(a[1])['encoding']
            if code!="ascii":
                a[1]=a[1].decode(code)
            dict[a[0]]=a[1]
        return dict
    

        